package com.ruoyi.project.system.record.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.monitor.online.domain.UserOnline;
import com.ruoyi.project.system.requirement.domain.Requirement;
import com.ruoyi.project.system.requirement.service.IRequirementService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.record.domain.WorkRecord;
import com.ruoyi.project.system.record.service.IWorkRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 工作日志Controller
 * 
 * @author ruoyi
 * @date 2019-11-15
 */
@Controller
@RequestMapping("/system/record")
public class WorkRecordController extends BaseController
{
    private String prefix = "system/record";

    @Autowired
    private IWorkRecordService workRecordService;
    @Autowired
    private IRequirementService requirementService;
    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询工作日志列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkRecord workRecord)
    {
        startPage();
        // 普通用户只显示分配给自己的需求
        boolean isDevUser = false;
        User user = getSysUser();
        List<Role> roles = user.getRoles();
        for(Role role : roles){
            if("common".equals(role.getRoleKey())){
                isDevUser = true;
                break;
            }
        }
        if(isDevUser){
            workRecord.setUserId(user.getUserId());
        }
        List<WorkRecord> list = workRecordService.selectWorkRecordList(workRecord);
        return getDataTable(list);
    }

    /**
     * 导出工作日志列表
     */
    @RequiresPermissions("system:record:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkRecord workRecord)
    {
        List<WorkRecord> list = workRecordService.selectWorkRecordList(workRecord);
        ExcelUtil<WorkRecord> util = new ExcelUtil<WorkRecord>(WorkRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 新增工作日志
     */
    @GetMapping("/add")
    public String add(WorkRecord workRecord,ModelMap mmap)
    {
        String workload = this.workRecordService.selectWorkRecordBySj(ShiroUtils.getSysUser().getUserId());
        Requirement requirement = this.requirementService.selectRequirementById(workRecord.getReqId());
        mmap.put("workRecord", workRecord);
        mmap.put("workload",workload);
        mmap.put("requirement",requirement);
        return prefix + "/add";
    }

    /**
     * 新增保存工作日志
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "工作日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkRecord workRecord,String reqStatus)
    {
        // 当前登录用户
        User user = ShiroUtils.getSysUser();
        workRecord.setUserId(user.getUserId());
        workRecord.setMarkDate(new Date());
        //更新需求状态
        Requirement requirement = this.requirementService.selectRequirementById(workRecord.getReqId());
        requirement.setReqStatus(reqStatus);
        this.requirementService.updateRequirement(requirement);
        return toAjax(workRecordService.insertWorkRecord(workRecord));
    }

    /**
     * 修改工作日志
     */
    @GetMapping("/edit/{workId}")
    public String edit(@PathVariable("workId") Long workId, ModelMap mmap)
    {
        WorkRecord workRecord = workRecordService.selectWorkRecordById(workId);
        mmap.put("workRecord", workRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存工作日志
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "工作日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkRecord workRecord)
    {
        return toAjax(workRecordService.updateWorkRecord(workRecord));
    }

    /**
     * 删除工作日志
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "工作日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workRecordService.deleteWorkRecordByIds(ids));
    }

    @PostMapping("/recordByReq")
    @ResponseBody
    public TableDataInfo recordByReq(WorkRecord workRecord)
    {
        startPage();
        List<WorkRecord> list = workRecordService.selectWorkRecordList(workRecord);
        return getDataTable(list);
    }
    /*
    *生成日报
    * */
    @GetMapping("/daily")
    public String daily(ModelMap modelmap)
    {
        String daily = "";
        WorkRecord  workRecord = new WorkRecord();
        workRecord.setUserId(ShiroUtils.getSysUser().getUserId());
        workRecord.setMarkDate(new Date());
        List<WorkRecord> workRecordList = this.workRecordService.selectWorkRecordList(workRecord);
       /* if(workRecordList!=null && workRecordList.size()>0){
            for (int i=0;i<workRecordList.size();i++){
                daily+=(i+1)+"、"+workRecordList.get(i).getWorkContent()+"（"+workRecordList.get(i).getWorkDays()+"天）\n";
            }
        }*/
        modelmap.addAttribute("workRecordList",workRecordList);
        return prefix + "/daily";
    }

}
